package org.lc.platform.mybatis.crud.controller;


import io.swagger.v3.oas.annotations.Operation;
import lombok.Getter;
import org.lc.platform.base.annotation.IsUUID;
import org.lc.platform.base.dto.DeleteManyDto;
import org.lc.platform.base.util.BeanValidatorUtil;
import org.lc.platform.mybatis.crud.service.CrudBaseService;
import org.lc.platform.base.dto.PageQuery;
import org.lc.platform.base.result.Result;
import org.lc.platform.base.vo.PageVo;
import org.lc.platform.swagger.interfaces.Update;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Getter
public class CrudBaseController<T, VO, Q extends PageQuery, S extends CrudBaseService<Q, T, VO>> {

    public final S service;

    public CrudBaseController(S service) {
        this.service = service;
    }

    @Operation(summary = "新增数据")
    @PostMapping()
    public Result<Boolean> createOne(@RequestBody T body) {
        BeanValidatorUtil.validate(body);
        return Result.data(this.service.createBaseOne(body));
    }

    @Operation(summary = "分页查询")
    @GetMapping()
    public Result<PageVo<VO>> getMany(Q query) {
        BeanValidatorUtil.validate(query);
        return Result.data(this.service.getBaseMany(query));
    }

    @Operation(summary = "单条查询")
    @GetMapping("{id}")
    public Result<VO> getOne(@IsUUID @PathVariable("id") String id, @RequestParam(value = "join", required = false) String join) {
        return Result.data(this.service.getBaseOne(id, join));
    }

    @Operation(summary = "单条更新")
    @PatchMapping("{id}")
    public Result<Boolean> updateOne(@IsUUID @PathVariable("id") String id, @Validated(value = {Update.class}) @RequestBody T data) {
        return Result.data(this.service.updateBaseOne(id, data));
    }

    @Operation(summary = "单条删除")
    @DeleteMapping("{id}")
    public Result<Boolean> deleteOne(@IsUUID @PathVariable("id") String id) {
        return Result.data(this.service.deleteBaseOne(id));
    }

    @Operation(summary = "批量删除")
    @PostMapping("/delete/many")
    public Result<Boolean> deleteMany(@RequestBody DeleteManyDto data) {
        return Result.data(this.service.deleteBaseMany(data.getIds()));
    }
}
